package com.mlnx.web.ui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mlnx.common.entity.Response;
import com.mlnx.common.form.PageForm;
import com.mlnx.device.entity.Device;
import com.mlnx.device.entity.EcgDevice;
import com.mlnx.device.enums.DeviceType;
import com.mlnx.device.enums.ECGDeviceRunMode;
import com.mlnx.device.pojo.dto.EcgDeviceDto;
import com.mlnx.device.pojo.form.DeviceForm;
import com.mlnx.device.service.*;
import com.mlnx.service.common.enums.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "UI设备接口", tags = "UI设备接口")
@RestController
@RequestMapping("ui/device")
public class DeviceUiController extends BaseController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private EcgDeviceService ecgDeviceService;
    @Autowired
    private BpDeviceService bpDeviceService;
    @Autowired
    private SugarDeviceService sugarDeviceService;
    @Autowired
    private SpoDeviceService spoDeviceService;


    @ApiOperation(value = "获取所有在线心电设备信息")
    @GetMapping(value = "/ecg/online")
    public Response getOnlineEcgDevices() {
        return result(ecgDeviceService.getOnlineEcgDevices());
    }

    @ApiOperation(value = "获取所有心电设备信息")
    @GetMapping(value = "/ecg")
    public Response getAllEcgDevices() {
        return result(ecgDeviceService.getAllEcgDevices());
    }

    @ApiOperation(value = "获取所有血压设备信息")
    @GetMapping(value = "/bp")
    public Response getAllBDevices() {
        PageForm pageForm = new PageForm();
        pageForm.setCurrent(1l);
        pageForm.setSize(1000l);
        return result(bpDeviceService.list(pageForm, null).getRecords());
    }

    @ApiOperation(value = "获取所有血糖设备信息")
    @GetMapping(value = "/bg")
    public Response getAllBgDevices() {
        PageForm pageForm = new PageForm();
        pageForm.setCurrent(1l);
        pageForm.setSize(1000l);
        return result(sugarDeviceService.list(pageForm, null).getRecords());
    }

    @ApiOperation(value = "获取所有血氧设备信息")
    @GetMapping(value = "/spo")
    public Response getAllSpoDevices() {
        PageForm pageForm = new PageForm();
        pageForm.setCurrent(1l);
        pageForm.setSize(1000l);
        return result(spoDeviceService.list(pageForm, null).getRecords());
    }

    @ApiOperation(value = "添加设备")
    @PostMapping(value = "")
    public Response addEcgDevice(@Valid DeviceForm deviceForm) {
        if (deviceForm.getDeviceType().equals(DeviceType.ECG_DEVICE.name()))
            if (!deviceForm.checkEcgDeviceParam())
                return result(ResponseEnum.ParamError);

        Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", deviceForm.getNumeration()));
        if (device != null)
            return result(ResponseEnum.DeviceExisted);

        deviceService.addDevice(deviceForm);

        return result();
    }

    @ApiOperation(value = "患者绑定设备")
    @PutMapping(value = "/{deviceId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true)
    })
    public Response bindDevice(@PathVariable("deviceId") String deviceId,
                               @RequestParam("patientId") Integer patientId) {
        deviceService.bindDevice(patientId, deviceId);

        return result();
    }

    @ApiOperation(value = "设置设备模式")
    @PutMapping(value = "/ecg/{deviceId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "runMode", value = "设备模式", required = true)
    })
    public Response setEcgDeviceRunMode(@PathVariable("deviceId") String deviceId,
                                        @RequestParam("runMode") String runMode) {
        Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", deviceId));
        if (device == null)
            return result(ResponseEnum.DeviceUnExisted);

        EcgDevice ecgDevice = new EcgDevice();
        ecgDevice.setId(device.getId());
        ecgDevice.setEcgDeviceRunMode(ECGDeviceRunMode.valueOf(runMode));

        ecgDeviceService.updateById(ecgDevice);

        return result();
    }

    @ApiOperation(value = "获取心电设备信息")
    @GetMapping(value = "/ecg/{deviceId}")
    public Response getEcgDeviceInfo(@PathVariable("deviceId") String deviceId) {
        EcgDeviceDto ecgDevice = ecgDeviceService.getEcgDeviceDto(deviceId);

        return result(ecgDevice);
    }

}
